package com.ruoyi.hospital.controller;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.hospital.domain.HospitalMultipartFile;
import com.ruoyi.hospital.entity.*;
import com.ruoyi.hospital.entity.check.*;
import com.ruoyi.hospital.entity.order.HospitalOrderDetailRet;
import com.ruoyi.hospital.entity.order.HospitalOrderReturnVO;
import com.ruoyi.hospital.entity.order.SendOrderReq;
import com.ruoyi.hospital.entity.schedule.ScheduleVO;
import com.ruoyi.hospital.service.HospitalCheckService;
import com.ruoyi.hospital.service.HospitalOrderService;
import com.ruoyi.hospital.service.HospitalTokenService;
import com.ruoyi.hospital.utils.HospitalServerConfig;
import com.ruoyi.hospital.utils.HospitalTokenFilter;
import com.ruoyi.service.domain.*;
import com.ruoyi.service.mapper.ServiceOrderotherpayMapper;
import com.ruoyi.service.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 订单业务控制
 *
 * @author ZDL
 */

@RestController
@RequestMapping("/appHospital/appHospitalOrder")
public class HospitalOrderController {

    @Autowired
    private IServiceOrdersService serviceOrdersService;

    @Autowired
    private HospitalTokenService tokenService;

    @Autowired
    private HospitalTokenFilter hospitalTokenFilter;

    @Autowired
    private HospitalOrderService hospitalOrderService;

    @Autowired
    private HospitalCheckService hospitalCheckService;

    @Autowired
    private IServicePictureService pictureService;

    @Autowired
    private IServiceComboService comboService;

    @Autowired
    private IServicePackagesService packagesService;

    @Autowired
    private IServicePictureService servicePictureService;

    @Autowired
    private IServiceOrdernurseService ordernurseService;

    @Autowired
    private ServiceOrderotherpayMapper orderotherpayMapper;

    @Autowired
    private HospitalServerConfig serverConfig;


    /**
     * 获取订单列表方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getOrderList")
    public AjaxResult getOrderList(String labelStatus, int page, int size, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            Page<HospitalOrderReturnVO> retOrderList = hospitalOrderService.getOrderList(labelStatus, oldHospital.getUserId(), page, size);

            return AjaxResult.success("获取成功", retOrderList);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 获取订单状态方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getSegmentsMain")
    public AjaxResult getSegmentsMain(HttpServletRequest request) {
        try {
            //if(!hospitalTokenFilter.doFilterInternal(request)) {
            //    AjaxResult ajaxResult = new AjaxResult();
            //    ajaxResult.put("code", 401);
            //    ajaxResult.put("msg", "用户信息过期，请重新登录");
            //    return ajaxResult;
            //}
            //LoginHospital oldHospital = tokenService.getLoginUser(request);
            //if (oldHospital.getUserId() == null) {
            //    return AjaxResult.warn("用户信息错误");
            //}
            List<OrderStatusRsp> segments = hospitalOrderService.getSegmentsMain();

            return AjaxResult.success("获取成功", segments);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 获取订单状态方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getSegments")
    public AjaxResult getSegments(HttpServletRequest request) {
        try {
            //if(!hospitalTokenFilter.doFilterInternal(request)) {
            //    AjaxResult ajaxResult = new AjaxResult();
            //    ajaxResult.put("code", 401);
            //    ajaxResult.put("msg", "用户信息过期，请重新登录");
            //    return ajaxResult;
            //}
            //LoginHospital oldHospital = tokenService.getLoginUser(request);
            //if (oldHospital.getUserId() == null) {
            //    return AjaxResult.warn("用户信息错误");
            //}
            List<OrderStatusWorkRsp> segments = hospitalOrderService.getSegments();

            return AjaxResult.success("获取成功", segments);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 获取订单列表方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getOrderListByNurse")
    public AjaxResult getOrderListByNurse(String labelStatus, int page, int size, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }

            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            Page<HospitalOrderReturnVO> retOrderList = hospitalOrderService.getOrderList(labelStatus, oldHospital.getUserId(), page, size);

            return AjaxResult.success("获取成功", retOrderList);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 获取订单详情方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getOrderDetial")
    public AjaxResult getOrderDetial(Long orderId, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if(orderId == null){
                return AjaxResult.warn("订单号错误");
            }

            HospitalOrderDetailRet orderDetial = hospitalOrderService.getOrderDetial(orderId, oldHospital);
            //orderDetial.setRoleId(oldHospital.getRoleId());
            //orderDetial.setRoleName(oldHospital.getRoleName());
            //orderDetial.setWardId(oldHospital.getWardId());
            //orderDetial.setWardName(oldHospital.getWardName());
            return AjaxResult.success("获取成功", orderDetial);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 派单方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/sendOrder")
    public AjaxResult sendOrder(@RequestBody SendOrderReq reqBody, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if (reqBody.getOrderId() == null){
                return AjaxResult.warn("订单号错误");
            }
            if (reqBody.getNurseIds() == null){
                return AjaxResult.warn("护士信息错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId(reqBody.getOrderId());
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 5){
                return AjaxResult.warn("订单状态现无法派单");
            }
            if (hospitalOrderService.sendOrder(reqBody, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("派单成功", data);
            } else {
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("派单失败", data);
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 获取可派单护士方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getNurseList")
    public AjaxResult getNurseList(Long orderId, String name, Long wardId, Long hospitalId, Integer page, Integer size, HttpServletRequest request ) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if(orderId == null){
                return AjaxResult.warn("订单号错误");
            }

            List<NurseReturnVO> nurseList = hospitalOrderService.getNurseList(orderId, name, oldHospital, page, size);
            //Page<NurseReturnVO> nurseList = hospitalOrderService.getNurseList(orderId, name, oldHospital, page, size);
            return AjaxResult.success("获取成功", nurseList);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 接单方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/acceptOrder")
    public AjaxResult acceptOrder(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.get("orderId");
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 7){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalOrderService.acceptOrder(orderId, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("接单成功", data);
            } else {
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("接单失败", data);
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 拒单方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/refuseOrder")
    public AjaxResult refuseOrder(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.get("orderId");
            Long reason = reqBody.get("reason");
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId(orderId);
            List<ServiceOrdernurse> ordernurseList = ordernurseService.selectByOrderId(oldOrder.getOrderId());
            if (oldHospital.getRoleId() == 1){
                if (oldOrder == null){
                    return AjaxResult.warn("订单信息错误");
                }
            }
            if (oldHospital.getRoleId() == 2){
                int flag = 0;
                for(ServiceOrdernurse ordernurse : ordernurseList){
                    if (ordernurse.getOrdernurseNurseid() == oldHospital.getUserId()){
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0 || oldOrder == null){
                    return AjaxResult.warn("订单信息错误");
                }
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0){
                if (oldHospital.getRoleId() == 1 && oldOrder.getOrderServiceflag() != 5)
                    return AjaxResult.warn("订单状态错误");
                if (oldHospital.getRoleId() == 2 && oldOrder.getOrderServiceflag() != 7)
                    return AjaxResult.warn("订单状态错误");
            }

            if (hospitalOrderService.refuseOrder(orderId, reason, 0, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("拒单成功", data);
            } else {
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("拒单失败", data);
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 转介方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/referralOrder")
    public AjaxResult referralOrder(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.get("orderId");
            Long reason = reqBody.get("reason");
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            List<ServiceOrdernurse> ordernurseList = ordernurseService.selectByOrderId(oldOrder.getOrderId());
            int flag = 0;
            for(ServiceOrdernurse ordernurse : ordernurseList){
                if (ordernurse.getOrdernurseNurseid() == oldHospital.getUserId()){
                    flag = 1;
                    break;
                }
            }
            if (flag == 0 || oldOrder == null){
                return AjaxResult.warn("订单信息错误");
            }

            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || (oldOrder.getOrderServiceflag() != 8 && oldOrder.getOrderServiceflag() != 9)){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalOrderService.refuseOrder(orderId, reason, 1, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("转介成功", data);
            } else {
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("转介失败", data);
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 出发方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/startOrder")
    public AjaxResult startOrder(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.get("orderId");
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 8){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalOrderService.startOrder(orderId, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("出发成功", data);
            } else {
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("出发失败", data);
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 到达方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/reachOrder")
    public AjaxResult reachOrder(@RequestBody Map<String, Object> reqBody,HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            String code = reqBody.get("code").toString();
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            if (code == null){
                return AjaxResult.warn("验证码错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 9){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalOrderService.reachOrder(orderId, code, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("到达成功", data);
            } else {
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("到达失败", data);
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 服务完成方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/endServiceOrder")
    public AjaxResult endServiceOrder(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 10){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalOrderService.endServiceOrder(orderId, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("服务完成", data);
            } else {
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("服务未完成", data);
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 得到服务费方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/getServiceCharge")
    public AjaxResult getServiceCharge(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId() ){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            ServiceChargeVO serviceChargeVO = hospitalCheckService.getServiceCharge(orderId, oldHospital);
            return AjaxResult.success(serviceChargeVO);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 得到耗材包费用方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/getPackageCharge")
    public AjaxResult getPackageCharge(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            PackageChargeVO packageChargeVO = hospitalCheckService.getPackageCharge(orderId, oldHospital);
            return AjaxResult.success(packageChargeVO);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 得到车程费方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/getTravelCharge")
    public AjaxResult getTravelCharge(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            TravelChargeVO travelChargeVO = hospitalCheckService.getTravelCharge(orderId, oldHospital);
            return AjaxResult.success(travelChargeVO);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 得到账单说明方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/getHospitalPicture")
    public AjaxResult getHospitalPicture(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.get("orderId");
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            List<PictureReturn> hospitalPicture = hospitalCheckService.getHospitalPicture(orderId, oldHospital);
            return AjaxResult.success(hospitalPicture);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 得到订单核对方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getOrderCheckInfo")
    public AjaxResult getOrderCheckInfo(Long orderId, HttpServletRequest request) {
        try {
            //Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId() ){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            CheckInfoRsp orderCheckInfo = hospitalCheckService.getOrderCheckInfo(orderId, oldHospital);
            return AjaxResult.success(orderCheckInfo);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 订单退款方法--暂时保存
     *
     * @param orderBody 订单操作信息
     * @return 结果
     */
    @PostMapping("/refundOrder")
    public AjaxResult refundOrder(@RequestBody HospitalOrderBody orderBody, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            if (!"0".equals(orderBody.getFlag())) {
                return AjaxResult.warn("操作无效");
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null || oldHospital.getUserId() != orderBody.getOrderUserid()) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderBody.getOrderId() == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId(orderBody.getOrderId());
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 3){
                return AjaxResult.warn("订单状态错误");
            }
            orderBody.setOrderPay(oldOrder.getOrderPay());
            if (hospitalOrderService.refundOrder(orderBody, oldHospital)){
                return AjaxResult.success("退款成功");
            } else {
                return AjaxResult.error("退款失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }




    /**
     * 订单护士评价方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/assessOrder")
    public AjaxResult assessOrder(@RequestBody Map<String, Object> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            String assessment = reqBody.get("assessment").toString();
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId(orderId);
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() < 11){
                return AjaxResult.warn("订单状态错误");
            }
            if (assessment == null) {
                return AjaxResult.warn("评价信息错误");
            }

            if (hospitalOrderService.assessOrder(orderId, assessment, oldHospital)){
                return AjaxResult.success("评价完成");
            } else {
                return AjaxResult.error("评价失败");
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 上传费用证明方法
     *
     * @param pictureBody 证明照片
     * @return 结果
     */
    @PostMapping("/saveHospitalPicture")
    public AjaxResult saveHospitalPicture(@RequestBody HospitalPictureRequest pictureBody, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if (pictureBody.getOrderId() == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId(pictureBody.getOrderId());
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (pictureBody.getImgUrls() == null) {
                return AjaxResult.warn("照片信息错误");
            }
            ServicePicture pictureSearch = new ServicePicture();
            pictureSearch.setPictureUserid(oldOrder.getOrderUserid());
            pictureSearch.setPictureOrderid(pictureBody.getOrderId());
            pictureSearch.setPictureType("2");
            pictureSearch.setPictureDelFlag("0");
            List<ServicePicture> nowPictureList = servicePictureService.selectServicePictureList(pictureSearch);
            if (nowPictureList.size() + pictureBody.getImgUrls().size() > 8){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.warn("上传数量超过上限", data);
            }
            ArrayList<String> tempImagesUrl = new ArrayList<String>();
            for(String imageUrl : pictureBody.getImgUrls()){
                tempImagesUrl.add(imageUrl.replace("@", "/"));
            }
            pictureBody.setImgUrls(tempImagesUrl);
            //List<String> tempImagesUrl = new ArrayList<String>();
            //for(String imageUrl : pictureBody.getImgUrls()){
            //    tempImagesUrl.add("https://img0.baidu.com/it/u=1024258047,2987031221&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=326");
            //}
            //pictureBody.setImgUrls(tempImagesUrl);

            if (hospitalCheckService.uploadHospitalPicture(pictureBody, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("上传成功", data);
            } else {
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("上传失败", data);
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除费用证明方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/deleteHospitalPicture")
    public AjaxResult deleteHospitalPicture(@RequestBody HospitalPictureRequest pictureBody, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if (pictureBody.getOrderId() == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId(pictureBody.getOrderId());
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId() ){
                return AjaxResult.warn("订单信息错误");
            }
            if (pictureBody.getImgUrls() == null) {
                return AjaxResult.warn("照片信息错误");
            }

            if (hospitalCheckService.deleteHospitalPicture(pictureBody, oldHospital)){
                return AjaxResult.success("删除成功");
            } else {
                return AjaxResult.error("删除失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 得到账单总额方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/getHospitalCharge")
    public AjaxResult getHospitalCharge(Long orderId, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            HospitalBillVO hospitalBillVO = hospitalCheckService.getHospitalCharge(orderId, oldHospital);
            return AjaxResult.success(hospitalBillVO);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增套餐方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/addCheckCombo")
    public AjaxResult addCheckCombo(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.get("orderId");
            Long comboId = reqBody.get("comboId");
            Long itemId = reqBody.get("itemId");
            Long count = reqBody.get("count");
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            if (itemId == null){
                return AjaxResult.warn("项目信息错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId() ){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }
            if (count == null){
                count = 1L;
            }
            if (comboId == null){
                return AjaxResult.warn("套餐信息错误");
            }
            ServiceCombo oldCombo = comboService.selectServiceComboByComboId(comboId);
            if (oldCombo == null || oldCombo.getComboUseFlag().equals("2")){
                return AjaxResult.warn("套餐信息无效");
            }

            if (hospitalCheckService.addCheckCombo(comboId, itemId, orderId, count, oldHospital)){
                return AjaxResult.success("添加成功");
            } else {
                return AjaxResult.error("添加失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增耗材包方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/addCheckPackage")
    public AjaxResult addCheckPackage(@RequestBody AddPackagesReq reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.getOrderId();
            //Long comboId = reqBody.get("comboId");
            List<AddPkgNodeReq> packageBody = reqBody.getPkgList();

            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }
            for (AddPkgNodeReq pkgNodeReq : packageBody){
                Long packageId = pkgNodeReq.getPkgId();
                Long count = pkgNodeReq.getNum();
                if (count == null){
                    count = 1L;
                }
                if (packageId == null){
                    return AjaxResult.warn("套餐信息错误");
                }
                ServicePackages oldPackage = packagesService.selectServicePackagesByPackagesId(packageId);
                if (oldPackage == null || oldPackage.getPackagesUseFlag().equals("2")){
                    return AjaxResult.warn("耗材包信息无效");
                }
            }
            if (hospitalCheckService.addCheckPackage(orderId, packageBody, oldHospital)){
                return AjaxResult.success("添加成功");
            } else {
                return AjaxResult.error("添加失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改新增套餐方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/updateCheckCombo")
    public AjaxResult updateCheckCombo(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.get("orderId");
            Long itemId = reqBody.get("itemId");
            Long comboId = reqBody.get("comboId");
            Long count = reqBody.get("count");
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }
            if (count == null){
                count = 0L;
            }
            if (comboId == null){
                return AjaxResult.warn("套餐信息错误");
            }
            ServiceCombo oldCombo = comboService.selectServiceComboByComboId(comboId);
            if (oldCombo == null || oldCombo.getComboUseFlag().equals("2")){
                return AjaxResult.warn("套餐信息无效");
            }

            if (hospitalCheckService.updateCheckCombo(comboId, itemId, orderId, count, oldHospital)){
                return AjaxResult.success("修改成功");
            } else {
                return AjaxResult.error("修改失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改新增耗材包方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/updateCheckPackage")
    public AjaxResult updateCheckPackage(@RequestBody Map<String, Long> reqBody, HttpServletRequest request) {
        try {
            Long orderId = reqBody.get("orderId");
            //Long comboId = reqBody.get("comboId");
            Long packageId = reqBody.get("packageId");
            Long count = reqBody.get("count");
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }
            if (count == null){
                count = 1L;
            }
            if (packageId == null){
                return AjaxResult.warn("耗材包信息错误");
            }
            ServicePackages oldPackage = packagesService.selectServicePackagesByPackagesId(packageId);
            if (oldPackage == null || oldPackage.getPackagesUseFlag().equals("2")){
                return AjaxResult.warn("耗材包信息无效");
            }

            if (hospitalCheckService.updateCheckPackage(packageId, orderId, count, oldHospital)){
                return AjaxResult.success("修改成功");
            } else {
                return AjaxResult.error("修改失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询套餐方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getComboListByPage")
    public AjaxResult getComboListByPage( int page, int size, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }

            Page<CheckItemsListReturnVO> categoryItemsListWithPagination = hospitalCheckService.getCategoryItemsListWithPagination(page, size);
            return AjaxResult.success(categoryItemsListWithPagination);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询耗材包方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getPackagesListByPage")
    public AjaxResult getPackagesListByPage( Long orderId, int page, int size, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if ( oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }

            Page<CheckPackageRsp> packageReturnHospitalPage = hospitalCheckService.getComboPackagesListWithPagination(orderId, page, size, oldHospital);
            return AjaxResult.success(packageReturnHospitalPage);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 取消核对方法  不使用
     *
     * @param
     * @return 结果
     */
    @PostMapping("/cancelAddNew")
    public AjaxResult cancelAddNew(@RequestBody Long orderId, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalCheckService.cancelAddNew(orderId, oldHospital)){
                return AjaxResult.success("取消核对成功");
            } else {
                return AjaxResult.error("取消核对失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 保存核对方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/saveAddNew")
    public AjaxResult saveAddNew(@RequestBody Map<String, Object> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            String remark = reqBody.get("remark").toString();
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalCheckService.saveAddNew(orderId, oldHospital)){
                return AjaxResult.success("保存核对成功");
            } else {
                return AjaxResult.error("保存核对失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 提交结算方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/checkOrders")
    public AjaxResult checkOrders(@RequestBody Map<String, Object> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            Long actualTaxiAmount = Long.valueOf(reqBody.get("actualTaxiAmount").toString());
            //Long goPay = Long.valueOf(reqBody.get("goPay").toString());
            Long goPay = actualTaxiAmount/2;
            //Long backPay = Long.valueOf(reqBody.get("backPay").toString());
            Long backPay = actualTaxiAmount - goPay;
            String remark = reqBody.get("remark").toString();
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null ) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            if (goPay == null || backPay == null){
                return AjaxResult.warn("结算信息错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId( orderId );
            if (oldOrder == null || oldOrder.getOrderNurseid() != oldHospital.getUserId()){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0 || oldOrder.getOrderServiceflag() != 11){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalCheckService.checkOrders(orderId, goPay, backPay, remark, oldHospital)){
                return AjaxResult.success("提交结算成功");
            } else {
                return AjaxResult.error("提交结算失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取订单拒绝或转介原因方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getNurseReason")
    public AjaxResult getNurseReason(HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }

            List<Map<String, Object>> nurseReason = hospitalOrderService.getNurseReason(oldHospital);
            return AjaxResult.success("获取成功", nurseReason);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 添加其他录入费用
     *
     * @param
     * @return 结果
     */
    @PostMapping("/addOthers")
    public AjaxResult addOthers(@RequestBody Map<String, Object> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            String desc = reqBody.get("desc").toString();
            Long price = Long.valueOf(reqBody.get("price").toString());
            Long count = Long.valueOf(reqBody.get("count").toString());
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            Long id = hospitalCheckService.addOthers(orderId, desc, price, count, oldHospital);
            if(id != null){
                Map<String, Object> data = new HashMap<>();
                data.put("id", id);
                data.put("flag", true);
                return AjaxResult.success("添加成功", data);
            }else{
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("添加失败", data);
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除其他录入费用
     *
     * @param
     * @return 结果
     */
    @PostMapping("/delOthers")
    public AjaxResult delOthers(@RequestBody Map<String, Object> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            Long otherId = Long.valueOf(reqBody.get("otherId").toString());
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            ServiceOrderotherpay oldOther = orderotherpayMapper.selectServiceOrderotherpayByOrderotherpayId(otherId);
            if (oldOther.getOrderotherpayOrderid() != orderId){
                return AjaxResult.warn("订单信息错误");
            }
            if(hospitalCheckService.delOthers(orderId, otherId, oldHospital)){
                Map<String, Object> data = new HashMap<>();
                data.put("flag", true);
                return AjaxResult.success("删除成功", data);
            }else{
                Map<String, Object> data = new HashMap<>();
                data.put("flag", false);
                return AjaxResult.error("删除失败", data);
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }
    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, HttpServletRequest request) throws Exception
    {
        try
        {
            if (!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * base64上传请求（单个-表单）
     */
    @PostMapping("/uploadForm")
    public AjaxResult uploadForm(Object file, HttpServletRequest request) {
        try {
            if (!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital == null || oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            String fileName = oldHospital.getUsername();
            String fileContent = file.toString();

            if (StringUtils.isEmpty(fileName) || StringUtils.isEmpty(fileContent)) {
                return AjaxResult.warn("文件信息错误");
            }

            // 去掉Base64前缀（例如 "data:image/jpeg;base64,"）
            String base64Image = fileContent.split(",")[1];
            byte[] imageBytes = Base64.getDecoder().decode(base64Image);

            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String newFileName = FileUtils.getName(fileName);
            String fullPath = filePath + newFileName;

            // 保存文件
            MultipartFile multipartFile = new HospitalMultipartFile(imageBytes, "file", fileName, "image/jpeg");

            // 上传并返回新文件名称
            fileName = FileUploadUtils.upload(filePath, multipartFile);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", multipartFile.getOriginalFilename());
            return ajax;

        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/uploadBase64")
    public AjaxResult uploadBase64File(@RequestBody Map<String, String> reqBody, HttpServletRequest request) {
        try {
            if (!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            String fileName = reqBody.get("fileName");
            String fileContent = reqBody.get("fileContent");

            if (StringUtils.isEmpty(fileName) || StringUtils.isEmpty(fileContent)) {
                return AjaxResult.warn("文件信息错误");
            }

            // 去掉Base64前缀（例如 "data:image/jpeg;base64,"）
            String base64Image = fileContent.split(",")[1];
            byte[] imageBytes = Base64.getDecoder().decode(base64Image);

            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String newFileName = FileUtils.getName(fileName);
            String fullPath = filePath + newFileName;

            // 保存文件
            MultipartFile multipartFile = new HospitalMultipartFile(imageBytes, "file", fileName, "image/jpeg");

            // 上传并返回新文件名称
            fileName = FileUploadUtils.upload(filePath, multipartFile);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", multipartFile.getOriginalFilename());
            return ajax;

        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 订单护士打卡拍照方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/saveLocPic")
    public AjaxResult saveLocPic(@RequestBody Map<String, Object> reqBody, HttpServletRequest request) {
        try {
            Long orderId = Long.parseLong(reqBody.get("orderId").toString());
            String pic = reqBody.get("pic").toString();
            String loc = reqBody.get("lon").toString();
            String location = reqBody.get("lat").toString();
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if (orderId == null){
                return AjaxResult.warn("订单号错误");
            }
            ServiceOrders oldOrder = serviceOrdersService.selectServiceOrdersByOrderId(orderId);
            if (oldOrder == null){
                return AjaxResult.warn("订单信息错误");
            }
            if (oldOrder.getOrderStatus() != 0 || oldOrder.getOrderFlag() != 0){
                return AjaxResult.warn("订单状态错误");
            }

            if (hospitalOrderService.saveLocPic(orderId, pic, loc, location, oldHospital)){
                return AjaxResult.success("保存完成");
            } else {
                return AjaxResult.error("保存失败");
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取月度日程方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getMonthSchedule")
    public AjaxResult getMonthSchedule(String year, String month, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if (year == null || month == null){
                return AjaxResult.warn("日期错误");
            }
            List<HashMap<String, Object>> schedule = hospitalOrderService.getMonthSchedule(year, month, oldHospital);
            return AjaxResult.success("获取成功", schedule);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 获取某日日程方法
     *
     * @param
     * @return 结果
     */
    @GetMapping("/getScheduleList")
    public AjaxResult getScheduleList(String date, HttpServletRequest request) {
        try {
            if(!hospitalTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            LoginHospital oldHospital = tokenService.getLoginUser(request);
            if (oldHospital.getUserId() == null) {
                return AjaxResult.warn("用户信息错误");
            }
            if (StringUtils.isEmpty(date)) {
                return AjaxResult.warn("日期错误");
            }
            List<ScheduleVO> schedule = hospitalOrderService.getScheduleList(date, oldHospital);
            return AjaxResult.success("获取成功", schedule);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

}
